/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.staff;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.AuthenticationDAO;
import database.RoleDAO;
import database.StaffAuthenticationDAO;
import database.StaffDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import model.Memberaccount;
import model.Role;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Staff")
public class AddStaffAction extends ActionSupport implements ModelDriven<Staff>{
    private Staff aStaff;
            //= new Staff();
    private StaffDAO staffDAO = new StaffDAO();
    public RoleDAO roleDAO = new RoleDAO();
    private List<Staff> staffs;
//    private String loginName;
//    private String password;
    private String role;
    private boolean administrator;
    private boolean newStaff;
    private boolean storekeeper;
    private boolean director;

    public StaffDAO getStaffDAO() {
        return staffDAO;
    }

    public void setStaffDAO(StaffDAO staffDAO) {
        this.staffDAO = staffDAO;
    }

    public boolean isAdministrator() {
        return administrator;
    }

    public void setAdministrator(boolean administrator) {
        this.administrator = administrator;
    }

    public boolean isNewStaff() {
        return newStaff;
    }

    public void setNewStaff(boolean newStaff) {
        this.newStaff = newStaff;
    }

    public boolean isStorekeeper() {
        return storekeeper;
    }

    public void setStorekeeper(boolean storekeeper) {
        this.storekeeper = storekeeper;
    }

    public boolean isDirector() {
        return director;
    }

    public void setDirector(boolean director) {
        this.director = director;
    }   

    public Staff getaStaff() {
        return aStaff;
    }

//    public String getLoginName() {
//        return loginName;
//    }
//
//    public String getPassword() {
//        return password;
//    }

    public String getRole() {
        return role;
    }

    /**
     * @param aStaff the aStaff to set
     */
    
    public void setaStaff(Staff aStaff) {
        this.aStaff = aStaff;
    }

    /**
     * @param loginName the loginName to set
     */
//    @RequiredStringValidator(message = "Login name is required.")
//    @StringLengthFieldValidator(message = "Login name must contain 6-30 characters.", minLength = "6", maxLength = "30")
//    public void setLoginName(String loginName) {
//        this.loginName = loginName;
//    }
//
//    /**
//     * @param password the password to set
//     */
//    @RequiredStringValidator(message = "Password is required.")
//    @StringLengthFieldValidator(message = "Password must be greater than 5 and smaller than 30.", minLength = "6", maxLength = "30")
//    public void setPassword(String password) {
//        this.password = password;
//    }

    /**
     * @param role the role to set
     */
    public void setRole(String role) {
        this.role = role;
    }
    @Override
    @Action(value = "addstaff", results = {
        @Result(name = "success", location = "/manageStaff/w_administrator.jsp"),
         @Result(name = "input", location = "/manageStaff/w_addstaff.jsp")
    })
    public String execute() throws Exception {
        String result;
           // System.out.println("administrator"+newStaff);
        //System.out.println("Trang"+aStaff);
        
        //System.out.println(role1);
        if (staffDAO.InsertStaff(aStaff)) {
            
            //Add staff history
//            Map session = ActionContext.getContext().getSession();
//            Staff staff = (Staff) session.get("staff");
//            new StaffAuthenticationDAO().addAction("add category", staff.getIdStaff());
           // System.out.println("administrator"+newStaff);
            addActionMessage("Add staff Successfully!");
            
            Integer idLast = staffDAO.getLastIdStaff();
            if(administrator == true)
            {
                for(int i = 1; i<= 10; i++)
                {
                    roleDAO.InsertStaffRole(idLast, i);
                }
                roleDAO.InsertStaffRole(idLast, 12);
            }
            else if(newStaff == true)
            {
                roleDAO.InsertStaffRole(idLast, 1);
                roleDAO.InsertStaffRole(idLast, 2);
                roleDAO.InsertStaffRole(idLast, 4);
                roleDAO.InsertStaffRole(idLast, 5);
                roleDAO.InsertStaffRole(idLast, 10);
            }
            else if(storekeeper == true)
            {
                roleDAO.InsertStaffRole(idLast, 6);
                roleDAO.InsertStaffRole(idLast, 7);
                roleDAO.InsertStaffRole(idLast, 8);
                roleDAO.InsertStaffRole(idLast, 10);
            }
            else if(director == true)
            {
                 for(int i = 1; i<= 12; i++)
                {
                    roleDAO.InsertStaffRole(idLast, i);
                }
            } 
            staffs = staffDAO.getAllStaff();
            result = SUCCESS;
        } else {
            addActionError("Register Failed (due to existed username or email).");
            result = //SUCCESS;
                    INPUT;
        }

        return result;
    }
    
    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Staff getModel() {
        return aStaff;
    }

    /**
     * @return the staffs
     */
    public List<Staff> getStaffs() {
        return staffs;
    }

    /**
     * @param staffs the staffs to set
     */
    public void setStaffs(List<Staff> staffs) {
        this.staffs = staffs;
    }
    
    
    
}
